# -- coding: utf-8 --
# @time : 2023/6/24
# @author : 周梦泽
# @file : pc_capture.py
# @software: pycharm
# 获取设备屏幕截图


import io
from PIL import ImageGrab
import base64


def screenshot_captured():
    # 获取屏幕截图
    screenshot = ImageGrab.grab()
    with io.BytesIO() as stream:
        # 将截图保存到IO流中,并自动关闭清理资源
        screenshot.save(stream, format="PNG")
        base64_str = "data:image/png;base64," + base64.b64encode(stream.getvalue()).decode("utf-8")
    return base64_str


if __name__ == '__main__':
    screenshot_captured()
